<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\goods\category\Group as CategoryGroup;
use app\common\model\order\Goods;
use app\common\model\order\Order;
use app\common\model\user\Group;
use app\common\model\user\Info;

/**
 * 我的区域管理接口
 */
class Region extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="first_cate", type="integer", required=true, sample="", description="一级分类")
     * @ApiParams (name="two_cate", type="integer", required=true, sample="", description="二级分类")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $params = request()->param();
        extract($params);
        $model = new Info();
        if (!isset($first_cate) || !$first_cate) {
            $this->error('参数错误');
        }
        if (!isset($two_cate) || !$two_cate) {
            $this->error('参数错误');
        }
        $model->where('first_cate', $first_cate);
        $model->where('two_cate', $two_cate);
        $model->where('user_id', $this->auth->id);
        $data = $model->with(['group'])->order('id desc')->find();
        $order_ids = Order::where('order_status', '>', '0')->whereTime('paytime', '>', $data->updatetime)->where('user_id', $this->auth->id)->column('id');
        $data->consumption = Goods::whereIn('order_id', $order_ids)->where('two_cate', $two_cate)->where('first_cate', $first_cate)->sum('pay_fee');
        $data->groups = $data->group_id == 2 ? CategoryGroup::where('category_id', $two_cate)->where('group_id', 3)->find() : '';
        if ($data->groups) {
            $set_the_time = $data->groups->set_the_time;
            
            if ($set_the_time > 0) {
                $data->groups->end_time = date('Y-m-d H:i', $data->updatetime + $set_the_time * 86400);
            } else {
                $data->groups->end_time = date('Y-m-d H:i', $data->updatetime);
            }
        }
        $data->cgroup =  CategoryGroup::where('category_id', $two_cate)->where('group_id', $data->group_id)->find();
        if ($data->cgroup) {
            $set_the_time = $data->cgroup->set_the_time;
            if ($set_the_time > 0) {
                $data->cgroup->end_time = date('Y-m-d H:i', $data->updatetime + $set_the_time * 86400);
            } else {
                $data->cgroup->end_time = date('Y-m-d H:i', $data->updatetime);
            }
        }
        $this->success('请求成功', $data);
    }
}
